/**
 * Project Name:mauto-web
 * File Name:JyWxXmAction.java
 * Package Name:com.inftech.mauto.jy.web
 * Date:2017年6月29日上午9:18:50
 * Copyright (c) 2017, inf-technology Co., Ltd. All Rights Reserved.
 *
*/

package com.inftech.mauto.jy.web;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.inftech.mauto.common.utils.CommonUtils;
import com.inftech.mauto.jy.entity.JyItemPjlb;
import com.inftech.mauto.jy.entity.JyItemWxxm;
import com.inftech.mauto.jy.service.JyItemPjlbService;
import com.inftech.mauto.jy.service.JyItemWxxmService;
import com.mingsoft.base.action.BaseAction;
import com.mingsoft.basic.constant.e.SessionConstEnum;
import com.mingsoft.basic.entity.ManagerSessionEntity;

import net.mingsoft.basic.util.BasicUtil;
import net.sf.json.JSONObject;

/**
 * ClassName:JyWxXmAction <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Date: 2017年6月29日 上午9:18:50 <br/>
 * 
 * @author Administrator
 * @version
 * @since JDK 1.7
 */
@Controller
@RequestMapping("/${managerPath}/wx")
public class JyWxXmAction extends BaseAction {

	@Resource(name = "jyItemWxxmServiceImpl")
	private JyItemWxxmService jyItemWxxmServiceImpl;

	@Resource
	private JyItemPjlbService jyItemPjlbServiceImpl;

	@RequestMapping("/wxxmList.do")
	public String getWxXm(HttpServletRequest request, HttpServletResponse response, String wxxmName) {
		List<JyItemPjlb> pJlist = jyItemPjlbServiceImpl.getPJLB();
		BasicUtil.startPage();
		List<Map<String, Object>> wxxmList = jyItemWxxmServiceImpl.getWxXmList(wxxmName);
		request.setAttribute("wxxmList", wxxmList);
		request.setAttribute("pJlist", pJlist);
		BasicUtil.endPage(wxxmList);
		request.setAttribute("pageNo",
				CommonUtils.isBlank(request.getParameter("pageNo")) ? 1 : request.getParameter("pageNo"));
		return "/WEB-INF/manager/mauto/jy/wxxm";
	}

	/**
	 * 
	 * save:(新增维修项目). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param wxxmName
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/save.do", produces = "application/jso;charset=utf-8")
	@ResponseBody
	public String save(HttpServletRequest request, HttpServletResponse response, String wxxmName) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			ManagerSessionEntity managerSession = (ManagerSessionEntity) getSession(request,
					SessionConstEnum.MANAGER_SESSION);
			Enumeration<String> enumeration = request.getParameterNames();
			String value = "";
			String name = "";
			Map<String, Object> map = new HashMap<String, Object>();
			while (enumeration.hasMoreElements()) {
				name = enumeration.nextElement();
				value = request.getParameter(name);
				map.put(name, value);
			}
			if (map.get("isZd") == null) {
				map.put("isZd", 1);
			}
			if (map.get("isOnly") == null) {
				map.put("isOnly", 1);
			}
			map.put("createBy", managerSession.getManagerId());
			jyItemWxxmServiceImpl.insertWxxm(map);
			resultMap.put("flag", 0);
			resultMap.put("resMsg", "插入成功");
		} catch (Exception e) {
			resultMap.put("flag", 1);
			resultMap.put("resMsg", "插入失败！");
		}
		return JSONObject.fromObject(resultMap).toString();
	}

	/**
	 * 
	 * allDeletePP:(删除维修项目). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param wxxmName
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping(value = "/allDeletePP.do", produces = "application/jso;charset=utf-8")
	@ResponseBody
	public String allDeletePP(HttpServletRequest request, HttpServletResponse response, String wxxmName) {
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			Enumeration<String> enumeration = request.getParameterNames();
			String value = "";
			String name = "";
			List<String> list = new ArrayList<String>();
			while (enumeration.hasMoreElements()) {
				name = enumeration.nextElement();
				value = request.getParameter(name);
				list.add(value);
			}
			int row = jyItemWxxmServiceImpl.updateWXXM(list);
			if (row == 0) {
				map.put("resMsg", "删除失败！");
				map.put("flag", "1");
			} else {
				map.put("resMsg", "删除成功！");
				map.put("flag", "0");
			}
		} catch (Exception e) {
			map.put("resMsg", "删除失败！");
			map.put("flag", "1");
		}
		return JSONObject.fromObject(map).toString();
	}

	/**
	 * 
	 * wxxmEdit:(编辑维修项目). <br/>
	 *
	 * @author Administrator
	 * @param request
	 * @param response
	 * @param wxxmName
	 * @return
	 * @since JDK 1.7
	 */
	@RequestMapping("/wxxmEdit.do")
	public String wxxmEdit(HttpServletRequest request, HttpServletResponse response, String id) {
		List<JyItemPjlb> pJlist = jyItemPjlbServiceImpl.getPJLB();
		JyItemWxxm jyItemWxxm = jyItemWxxmServiceImpl.getWxXmListById(id);
		request.setAttribute("pJlist", pJlist);
		request.setAttribute("jyItemWxxm", jyItemWxxm);
		return "/WEB-INF/manager/mauto/jy/editWxxm";
	}

	@RequestMapping(value = "/update.do", produces = "application/jso;charset=utf-8")
	@ResponseBody
	public String update(HttpServletRequest request, HttpServletResponse response,
			@RequestParam Map<String, Object> param) {
		Map<String, Object> map = new HashMap<String, Object>();
		ManagerSessionEntity managerSession = (ManagerSessionEntity) getSession(request,
				SessionConstEnum.MANAGER_SESSION);
		param.put("updateBy", managerSession.getManagerId());
		try {
			int row = jyItemWxxmServiceImpl.updateWXXmById(param);
			if (row == 0) {
				map.put("resMsg", "编辑失败！");
				map.put("flag", "1");
			} else {
				map.put("resMsg", "编辑成功！");
				map.put("flag", "0");
			}
		} catch (Exception e) {
			map.put("resMsg", "编辑失败！");
			map.put("flag", "1");
		}
		return JSONObject.fromObject(map).toString();
	}

}
